'use strict';

/**
 * @name SystemUpdateCtrl
 * @description 系统更新
 * @author Yud
 */

angular.module('metalabOperationApp').controller('SystemUpdateCtrl', ['$scope', '$stateParams', '$rootScope', '$state', 'params', 'MetaResources', 'MetaRemind', '$uibModalInstance', function($scope, $stateParams, $rootScope, $state, params, MetaResources, MetaRemind, $uibModalInstance) {
    if (params.flag === "0") {
        $scope.backup = "";
        $scope.content = "";
        $scope.title = "创建更新日志";
        $scope.positiveBtn = "保 存";
        $scope.negativeBtn = "重 置";
    } else if (params.flag === "1") {
        $scope.backup = params.data.content;
        $scope.content = params.data.content;
        $scope.title = "更新历史日志";
        $scope.positiveBtn = "更 新";
        $scope.negativeBtn = "重 置";
    }
    $scope.sure = function() {
        if (params.flag === "0") {
            if ($scope.content !== "") {
                MetaResources.update.create({}, {
                        'content': $scope.content,
                        'tag': '更新日志'
                    },
                    function() {
                        MetaRemind.success("恭喜", "创建日志成功");
                        $uibModalInstance.dismiss('cancel');
                        $state.reload();
                    },
                    function(error) {
                        MetaRemind.serverError(error);
                        $uibModalInstance.dismiss('cancel');
                    });
            } else {
                MetaRemind.error("失败", "更新日志不得为空");
            }
        } else if (params.flag === "1") {
            if ($scope.content !== "") {
                MetaResources.update.update({
                        'id': params.data.id
                    }, {
                        'content': $scope.content
                    },
                    function() {
                        MetaRemind.success("恭喜", "创建日志成功");
                        $uibModalInstance.dismiss('cancel');
                        $state.reload();
                    },
                    function(error) {
                        MetaRemind.serverError(error);
                        $uibModalInstance.dismiss('cancel');
                    });
            } else {
                MetaRemind.error("失败", "编辑历史日志不得为空");
            }
        }

    };
    $scope.reset = function() {
        $scope.content = angular.copy($scope.backup);
    };
    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };

    $scope.init = function() {
        $scope.backup = angular.copy($scope.content);
    };
}]);
